<template>
	<view class="index1_content">
		<view class="search-header">
			<view @click="handeleTo">
				<image style="width: 15rpx;height: 30rpx;" src="@/static/index2/index7.png" mode=""></image>
			</view>
			<view style="color: #FFFFFF;font-size: 32rpx;margin-right: 10rpx;">{{$t('货位管理')}}</view>
			<view></view>
		</view>
		<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Ys" refresher-enabled="true"
			:refresher-triggered="freshIsShow" @scrolltolower="lower" @refresherrefresh="fresh"
			@refresherrestore="store"
			refresher-background="background: linear-gradient(90deg, #5695FF -2%, #44D1D4 105%);">
			<view class="location-block" v-for="(item,index) in model" :key="index">
				<view class="location-block1">
					<view>{{$t('所属仓库')}}</view>
					<view style="color: #999999;">{{w_name}}</view>
				</view>
				<view style="border-top: 1px solid #D8D8D8;margin-top: 16rpx;"></view>
				<view class="location-block2">{{$t('货位名称')}}</view>
				<view class="location-block3">
					<u-input :focus="!item.isDisabled" :disabled="item.isDisabled" @blur="handeleBlur(index)"
						style="font-size: 24rpx;margin-right: 50rpx;" v-model="item.name"></u-input>
					<view style="display: flex;align-items: center;justify-content: flex-end;">
						<image @click.stop="handeleEdit(index)" style="width: 34rpx;height: 34rpx;"
							src="@/static/index2/index15.png" mode=""></image>
						<image @click.stop="handeleRemove(index)" style="width: 34rpx;height: 34rpx;margin-left: 18rpx;"
							src="@/static/index2/index16.png" mode=""></image>
					</view>
				</view>
			</view>

			<view v-if="model.length===0" class="nomore">
				<view>没有更多了</view>
			</view>
		</scroll-view>

		<view class="location-submit">
			<view class="location-but" @click="handeleSubmit">{{$t("新增货位")}}</view>
		</view>
		<!-- 顶部背景色 -->
		<image style="width: 750rpx;height: 690rpx;position: absolute;top: 0;z-index: 1;"
			src="@/static/index2/20241101130857.png" mode=""></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				w_name: "",
				w_id: null,
				model: [],
				scrollTop: 0, //高度
				freshIsShow: true, //下拉加载
				page: 1, //页数
				total: 0, //总条数
				arr: [], //数据
				warehouse: []
			}
		},
		onLoad(options) {
			this.w_id = options.w_id
			this.w_name = options.warehouse
			this.getData();
		},
		methods: {
			getData() {
				this.$u.api.basic.storage_lists({
					user_id: uni.getStorageSync("user_id"),
					w_id: this.w_id,
					page: 1,
					limit: 9999
				}).then(res => {
					res.forEach(item => {
						item.isDisabled = true;
					})
					this.model = res;
					this.freshIsShow = false
				})
			},
			// 下拉刷新复位
			store() {
				this.freshIsShow = false
			},
			// 下拉刷新触发
			fresh() {
				this.page = 1
				this.freshIsShow = true
				this.getData()
			},
			// 触底加载
			lower() {
				// 触底判断总条数不等于当前数据的条数则获取数据
				if (this.total != this.arr.length) {
					this.getData()
				}
			},
			// 新增货位
			handeleSubmit() {
				this.model.push({
					name: '',
					isDisabled: false
				})
			},
			// 删除
			handeleRemove(index) {
				uni.showModal({
					title: this.$t('确认删除'),
					content: this.$t('是否确认删除当前'),
					confirmText: this.$t('确认'),
					cancelText: this.$t('取消'),
					success: res => {
						if (res.confirm) {

							//调删除接口
							this.$u.api.basic.storage_del({
								id: this.model[index].id
							}).then(res => {
								uni.showToast({
									title: this.$t("操作成功")
								})
								this.model.splice(index, 1)
							})


						} else if (res.cancel) {}
					}
				});
			},
			// 编辑
			handeleEdit(index) {
				this.model[index].isDisabled = false
			},
			// 失去焦点调接口
			handeleBlur(index) {
				if (this.model[index].name) {
					if (String(this.model[index].name).length < 2) {
						uni.showToast({
							icon: 'none',
							title: this.$t('货位名称') + this.$t('长度不能小于') + 2
						})
						return
					}
					if (String(this.model[index].name).length > 20) {
						uni.showToast({
							icon: 'none',
							title: this.$t('货位名称') + this.$t('长度不能大于') + 20
						})
						return
					}
					this.$u.api.basic.storage_save({
						user_id: uni.getStorageSync("user_id"),
						id: this.model[index].id,
						w_id: this.w_id,
						name: this.model[index].name,
					}).then(res => {
						uni.showToast({
							title: this.$t("操作成功")
						})
						this.model[index].isDisabled = true
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: '货位名称不能为空'
					})
				}
			},
			handeleTo() {
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped>
	.nomore {
		color: #999;
		font-size: 26rpx;
		text-align: center;
		display: flex;
		align-items: center;
		justify-content: center;

		view {
			background: #f1f2f4;
			border-radius: 30rpx;
			padding: 10rpx
		}
	}

	.scroll-Ys {
		margin-top: 24rpx;
		z-index: 2;
		width: 690rpx;
		height: calc(100vh - 340rpx);
	}

	.location-but {
		width: 540rpx;
		height: 90rpx;
		color: #FFFFFF;
		font-size: 28rpx;
		background: #5282E6;
		border-radius: 50rpx;
		text-align: center;
		line-height: 90rpx;
	}

	.location-submit {
		z-index: 2;
		width: 100%;
		height: 140rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #F1F2F4;
		position: absolute;
		bottom: 0;
	}

	.location-block3 {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 16rpx;
		font-size: 22rpx;
		color: #999999;
	}

	.location-block2 {
		width: 100%;
		height: 25rpx;
		margin-top: 20rpx;
		font-size: 24rpx;
		color: #1D1D1D;
	}

	.location-block1 {
		width: 100%;
		height: 25rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 24rpx;

	}

	.location-block {
		z-index: 2;
		width: 690rpx;
		min-height: 174rpx;
		border-radius: 32rpx;
		background: #FFFFFF;
		padding: 30rpx 30rpx 22rpx 30rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
	}

	.search-header {
		z-index: 2;
		width: 690rpx;
		height: 133rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 40rpx;
	}

	.index1_content {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	page {
		width: 100%;
		height: 100%;
		background-color: #F1F2F4;
	}
</style>